package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.constant.RedisConstant;
import com.clhbolg.domain.WebsiteConfig;
import com.clhbolg.domain.bo.WebsiteConfigBo;
import com.clhbolg.domain.vo.WebsiteConfigVo;
import com.clhbolg.mapper.WebsiteConfigMapper;
import com.clhbolg.model.dto.WebsiteConfigDTO;
import com.clhbolg.model.vo.request.WebsiteConfigVO;
import com.clhbolg.service.IWebsiteConfigService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class WebsiteConfigServiceImpl implements IWebsiteConfigService {

    private final WebsiteConfigMapper baseMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public WebsiteConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<WebsiteConfigVo> queryPageList(WebsiteConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WebsiteConfig> lqw = buildQueryWrapper(bo);
        Page<WebsiteConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<WebsiteConfigVo> queryList(WebsiteConfigBo bo) {
        LambdaQueryWrapper<WebsiteConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WebsiteConfig> buildQueryWrapper(WebsiteConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WebsiteConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getConfig()), WebsiteConfig::getConfig, bo.getConfig());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(WebsiteConfigBo bo) {
        WebsiteConfig add = BeanUtil.toBean(bo, WebsiteConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(WebsiteConfigBo bo) {
        WebsiteConfig update = BeanUtil.toBean(bo, WebsiteConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WebsiteConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public WebsiteConfigDTO getWebsiteConfig() {
        WebsiteConfigDTO websiteConfigDTO;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object websiteConfig = valueOperations.get(RedisConstant.WEBSITE_CONFIG);
        if (Objects.nonNull(websiteConfig)) {
            websiteConfigDTO = JSONUtil.toBean(websiteConfig.toString(), WebsiteConfigDTO.class);
        } else {
            String config = baseMapper.selectById(1).getConfig();
            websiteConfigDTO = JSONUtil.toBean(config, WebsiteConfigDTO.class);
            valueOperations.set(RedisConstant.WEBSITE_CONFIG, config);
        }
        return websiteConfigDTO;
    }

    @Override
    @Transactional
    public void updateWebsiteConfig(WebsiteConfigVO websiteConfigVO) {
        WebsiteConfig websiteConfig = WebsiteConfig.builder()
            .id(1L)
            .config(JSONUtil.toJsonStr(websiteConfigVO))
            .build();
        baseMapper.updateById(websiteConfig);
        redisTemplate.delete(RedisConstant.WEBSITE_CONFIG);
    }

}
